<?php
require_once("/var/www/html/alipay/alipay.config.php");
require_once("/var/www/html/alipay/lib/alipay_notify.class.php");

function l_r($word) {
    $filepath="/var/www/html/new/d/logs/rech_log.txt";
    $maxsize  = 1024*1024*10;
    $filesize = filesize($filepath);
    if($filesize>$maxsize){
		$filetype = end(explode(".",$filepath)); //获取文件后缀名
		$filename = substr($filepath,0,strpos($filepath,'.'));
        rename($filepath, $filename.'_'.date('Ymd').'.'.$filetype);
        $handler=fopen($filepath,'w');
        flock($handler, LOCK_EX);
        fwrite($handler,"执行日期：".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");
        fclose($handler);
    } 
    else{
		$fp = fopen($filepath,"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,"执行日期：".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
    }
}
//计算得出通知验证结果
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify();

if($verify_result) {//验证成功
	
	
	write_logs1(json_encode($_POST));
	$out_trade_no = $_POST['out_trade_no'];// 商户订单号
	$trade_no = $_POST['trade_no'];        // 支付宝交易号
	$trade_status = $_POST['trade_status'];// 交易状态


    if($_POST['trade_status'] == 'TRADE_FINISHED') {

        logResult("1:$trade_no:$out_trade_no:$trade_status\n");
		
    }elseif($_POST['trade_status'] == 'TRADE_SUCCESS') {

    }

	//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
	include_once("/var/www/html/new/com.inc.php");
    $sel="select status,description from pft_alipay_rec where out_trade_no='".$out_trade_no."' limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();
    $total_fee = $_POST['total_fee']*100;
	if($GLOBALS['le']->f("status")==0){
		$body_a = explode('|', $_POST['body']);
		$mask   = $body_a[0];// 备注
		$did=explode("|",$GLOBALS['le']->f("description"));
		$ac=$GLOBALS['_ac'];
		$pw=$GLOBALS['_pw'];
		$s = new SoapClient(null,array(
             "location" => "http://localhost/open/openService/pft_insideMX.php",
            "uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
		if(($res=$s->PFT_Member_Fund_Modify($did[1],$did[1],$total_fee,0,1,$did[2],4,2,'',$mask))!=100){
			l_r($out_trade_no.":".$res);
		}
		else{
			
			
            //票付通总账号加钱——2015年4月13日11:56:33林农说要添加，宝椿提供的代码
//            $s->PFT_Member_Fund_Modify(1, 0, $total_fee, 0, 0,null,3, 0,$out_trade_no, $mask);
			$upd="update pft_alipay_rec set dtime=now(),status=1 where out_trade_no='".$out_trade_no."' limit 1";
			$GLOBALS['le']->query($upd);
			$did[3] = 1;// 供应商提供支付宝账号也没用。所有的都要给供应商账户加钱 2015/05/11 abc
			if($did[3]){
				if(($res=$s->PFT_Member_Fund_Modify($did[2],$did[1],$total_fee,0,0,"",3,0,'', $mask))!=100){
					l_r($out_trade_no."转入供应商的资金账户时:".$res);
				}
			}
			else{
				$sel="select alipay from pft_member where id=".$did[1]." limit 1";
				$GLOBALS['le']->query($sel);
				$GLOBALS['le']->fetch_assoc();
				if(!$GLOBALS['le']->f("alipay")){
					$upd="update pft_member set alipay='".$_POST['buyer_email']."' where id=".$did[1]." limit 1";
					$GLOBALS['le']->query($upd);
				}
			}
            //票付通总账号扣钱
            $s->PFT_Member_Fund_Modify(1, 0, $total_fee, 1, 0,null,3, 0,$out_trade_no, $mask);

        }
	}
	
	echo "success";		//请不要修改或删除
	logResult("2:$trade_no:$out_trade_no:$trade_status\n");

	
	
}else {
    //验证失败
    echo "fail";
	logResult("3:$trade_no:$out_trade_no:$trade_status\n");
}


function write_logs1($txt,$file="/var/www/html/new/d/api/logs/data_111.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}